const testMode = false; // 为true时可以在浏览器打开不报错
// vscode webview 网页和普通网页的唯一区别：多了一个acquireVsCodeApi方法
const vscode = testMode ? {} : acquireVsCodeApi();
const callbacks = {};

/**
 * 调用vscode原生api
 * @param data 可以是类似 {cmd: 'xxx', param1: 'xxx'}，也可以直接是 cmd 字符串
 * @param cb 可选的回调函数
 */
function callVscode (data, cb) {
  if (typeof data === 'string') {
    data = { cmd: data };
  }
  if (cb) {
    // 时间戳加上5位随机数
    const cbid = Date.now() + '' + Math.round(Math.random() * 100000);
    callbacks[cbid] = cb;
    data.cbid = cbid;
  }
  vscode.postMessage(data);
}

window.addEventListener('message', event => {
  const message = event.data;
  switch (message.cmd) {
    case 'vscodeCallback':
      console.log(message.data);
      (callbacks[message.cbid] || function () { })(message.data);
      delete callbacks[message.cbid];
      break;
    default: break;
  }
});


new Vue({
  el: '#app',
  data: {
    projectName: '加载中',
    writer: '',
    environment: ''
  },
  mounted () {
    this.readPackageJson();
    this.initJsonConfig();
  },
  watch: {
  },
  methods: {
    readPackageJson () {
      callVscode({ cmd: 'readPackageJson' }, (msg) => {
        this.projectName = msg.text;
      });
    },
    //项目初始化配置文件
    initJsonConfig () {
      callVscode({ cmd: 'initJsonConfig' }, (msg) => {
        this.writer = msg.text.name;
        this.environment = msg.text.environment;
      });
    },
    // 模拟alert
    alert (info) {
      callVscode({ cmd: 'alert', info: info }, null);
    },
    // 弹出错误提示
    error (info) {
      callVscode({ cmd: 'error', info: info }, null);
    },
    openUrlInBrowser () {
      callVscode({ cmd: 'openUrlInBrowser', url: `http://1.117.235.17/Vue2.0/index.html#/Home` }, () => {
        this.alert('打开天好组件库成功！');
      });
    },
    openFileInVscode () {
      callVscode({ cmd: 'openFileInVscode', path: `package.json` }, () => {
        this.alert('打开package.json成功！');
      });
    },
    openFileInSkyConfig () {
      const option = {
        writer: this.writer,
        environment: this.environment
      };
      callVscode({ cmd: 'openFileInSkyConfig', path: `sky.config.json`, option: option }, () => {
        this.alert('打开sky.config.json成功！');
      });
    }
  }
});